import jsonpath

from mall.utilsb.get_keyword import GetKeyWord
from mall.utilsb.send_method import SendMethod


class MemberInterface:
    def __init__(self):
        self.url = 'http://47.108.206.100:8085'

    def get_auth_code(self, telephone):
        method = 'get'
        url = self.url + '/sso/getAuthCode'
        payload = {
            'telephone': telephone
        }
        return SendMethod.send_method(method=method, url=url, params=payload)

    def get_verify_code(self, tel):
        response = self.get_auth_code(tel)
        return GetKeyWord.get_keyword(response, 'data')

    def register(self, tel, username, password):
        method = 'post'
        url = self.url + '/sso/register'
        data = {
            'authCode': self.get_verify_code(tel=tel),
            'password': password,
            'telephone': tel,
            'username': username
        }
        return SendMethod.send_method(method=method, url=url, data=data)

    def login(self, password, username):
        method = 'post'
        url = self.url + '/sso/login'
        data = {
            'password': password,
            'username': username
        }
        return SendMethod.send_method(method=method, url=url, data=data)

    def get_token(self, password, username):
        data=self.login(password,username)
        token = GetKeyWord.get_keyword(data, 'token')
        if not token:
            exit('账号密码错误')
        tokenhead = GetKeyWord.get_keyword(data, 'tokenHead')
        Authorization = str(tokenhead) + str(token)

        return Authorization

    def get_user_info(self, password, username):
        method = 'get'
        url = self.url + '/sso/info'
        Authorization = self.get_token(password,username)
        headers = {
            "Authorization": Authorization
        }
        info = SendMethod.send_method(method=method, url=url, headers=headers)
        return GetKeyWord.get_keyword(info, 'data')

    def change_password(self, tel, password):
        method = 'post'
        url = self.url + '/sso/updatePassword'
        authcode = self.get_verify_code(tel)
        data = {
            'authCode': authcode,
            'password': password,
            'telephone': tel
        }
        return SendMethod.send_method(method=method, url=url, data=data)


if __name__ == '__main__':
    # print(MemberInterface().register('15195770699','lindajin','123456'))
    # print(MemberInterface().get_verify_code(12345678912))
    # print(GetKeyWord.get_keyword(MemberInterface().login('123123', 'jerr'), 'code'))
    # print(MemberInterface().get_user_in'123456', 'lindajin'fo('123456', 'lindajin'))
    # print(MemberInterface().change_password('15195770699', '123123'))
    # print(MemberInterface().get_token('123123', 'lindajin'))
    print(MemberInterface().get_user_info('123123', 'lindajin'))
